package cn.zhaoyuening.chat_client.ui;

import cn.zhaoyuening.chat_client.client.ChatClient;
import cn.zhaoyuening.chat_client.utils.ClientConstant;
import cn.zhaoyuening.chat_common.msg.ChatMsg;
import cn.zhaoyuening.zamh.handler.MsgHandler;
import cn.zhaoyuening.zamh.manager.MsgManager;
import cn.zhaoyuening.zamh.manager.MsgManagers;
import cn.zhaoyuening.zamh.msg.Msg;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * Created by Buynow on 2017/6/14.
 */
public class ChatUi {
    private JPanel main;
    private JTextField usernameField;
    private JTextPane showChatMsg;
    private JTextArea msgContent;
    private JButton sendMsgBtn;
    private JLabel usernameLab;
    private JButton connectServerButton;
    private JTextField hostField;
    private JTextField portField;
    private ChatClient client;
    private MsgManager msgManager = MsgManagers.getBaseMsgManager();

    public void init() {
        JFrame frame = new JFrame("Ui");
        frame.setContentPane(main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        hostField.setText("127.0.0.1");
        portField.setText("8080");
        msgContent.setText("content test!");
        usernameField.setText("zhao");
        msgManager.addHandler(new MsgHandler(ClientConstant.ACCEPT_CHAT_MSG) {
            @Override
            public void handle(Msg msg) {
                if (msg.getInfo() instanceof ChatMsg) {
                    ChatMsg chatMsg = (ChatMsg) msg.getInfo();
                    String content = chatMsg.getContent();
                    String username1 = chatMsg.getUsername();
                    Date date = chatMsg.getDate();
                    //显示在content view
                    String text = showChatMsg.getText();
                    text += "\n" + username1 + " :" + date;
                    text += "\n" + content;
                    showChatMsg.setText(text);
                }
            }
        });
        this.connectServerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //获取用户名
                String usernameStr = usernameField.getText();
                //获取host portField
                String host = hostField.getText();
                String post = portField.getText();
                int port = Integer.parseInt(post);
                client = new ChatClient(host, port, msgManager);
                new Thread(client).start();
            }
        });
        this.sendMsgBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String content = msgContent.getText();
                ChatMsg chatMsg = new ChatMsg();
                chatMsg.setUsername(usernameField.getText());
                chatMsg.setDate(new Date());
                chatMsg.setContent(content);
                msgManager.sendMsg(new Msg(ClientConstant.SEND_CHAT_MSG, chatMsg));
            }
        });
    }

    public JPanel getMain() {
        return main;
    }

    public static void main(String[] args) {
        ChatUi ui = new ChatUi();
        ui.init();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        main = new JPanel();
        main.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(7, 2, new Insets(0, 0, 0, 0), -1, -1));
        usernameField = new JTextField();
        main.add(usernameField, new com.intellij.uiDesigner.core.GridConstraints(2, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        usernameLab = new JLabel();
        usernameLab.setText("username");
        main.add(usernameLab, new com.intellij.uiDesigner.core.GridConstraints(2, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane();
        main.add(scrollPane1, new com.intellij.uiDesigner.core.GridConstraints(3, 0, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        showChatMsg = new JTextPane();
        showChatMsg.setText("");
        scrollPane1.setViewportView(showChatMsg);
        msgContent = new JTextArea();
        msgContent.setText("");
        main.add(msgContent, new com.intellij.uiDesigner.core.GridConstraints(4, 0, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
        sendMsgBtn = new JButton();
        sendMsgBtn.setText("发送消息");
        main.add(sendMsgBtn, new com.intellij.uiDesigner.core.GridConstraints(6, 0, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        connectServerButton = new JButton();
        connectServerButton.setText("connectServer");
        main.add(connectServerButton, new com.intellij.uiDesigner.core.GridConstraints(5, 0, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        hostField = new JTextField();
        main.add(hostField, new com.intellij.uiDesigner.core.GridConstraints(0, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("host");
        main.add(label1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("port");
        main.add(label2, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_NONE, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        portField = new JTextField();
        main.add(portField, new com.intellij.uiDesigner.core.GridConstraints(1, 1, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return main;
    }
}
